package com.hydra.wiki.graph.entity.view;

import com.hydra.wiki.graph.entity.Entity;
import com.hydra.wiki.graph.entity.EntityRel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Optional;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class View {

    private String name;

    private String desc;

    private String source;

    private String target;

    public static View fromEntity(Entity entity){
        View view = null;
        if (entity != null){
            view = View.builder()
                    .name(entity.getName())
                    .desc(entity.getDesc())
                    .build();
        }
        return view;
    }

    public static View fromEntityRel(EntityRel entityRel){
        View view = null;
        if (entityRel != null){
            view = View.builder()
                    .name(entityRel.getName())
                    .source(Optional.ofNullable(entityRel.getStartEntity()).map(Entity::getName).orElse(null))
                    .target(Optional.ofNullable(entityRel.getEndEntity()).map(Entity::getName).orElse(null))
                    .build();
        }
        return view;
    }
}
